package com.ddstation.app.invssettings.controller;

import com.ddstation.app.invssettings.bean.SettingModifyBean;
import com.ddstation.app.invssettings.service.InvsSettingsService;
import com.ddstation.common.util.CheckUtil;
import com.ddstation.common.util.ClassConvertUtil;
import com.ddstation.crud.entity.DtInvsTxtStorageEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
public class InvsSettingsController {
    private static final Logger LOGGER = LoggerFactory.getLogger(InvsSettingsController.class);

    @Inject private InvsSettingsService invsSettingsService;

    /**
     * 根据institutionId获取机构对应的卫监设置信息
     * @param params 参数Map
     * @return 查询结果
     */
    @RequestMapping(value = "appinvs_invs_settings.do/load", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getSelfInvsSettings(@RequestBody Map<String, Object> params) {
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("errCode", -1);
        if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))) {
            return retMap;
        }
        String institutionId = params.get("institutionId").toString();
        retMap.put("settings", invsSettingsService.getInvsSettings(institutionId));
        retMap.put("errCode", 0);
        return retMap;
    }

    /**
     * 修改 卫监设置
     * @param params 参数Map
     * @return 操作结果
     */
    @RequestMapping(value = "appinvs_invs_settings.do/modify", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> modifyInvsSettings(@RequestBody Map<String, Object> params) {
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("errCode", -1);
        if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId")) || CheckUtil.isEmpty(params.get("doctorId"))) {

            return retMap;
        }
        SettingModifyBean modifyBean = ClassConvertUtil.convertMapToClass(params, SettingModifyBean.class);
        // 前端传的doctorId为字符串格式
        modifyBean.setDoctorId(Integer.parseInt(params.get("doctorId").toString()));
        if (!checkBean(modifyBean)) {
            return retMap;
        }

        if (invsSettingsService.modifySetting(modifyBean)) {
            retMap.put("errCode", 0);
            retMap.put("result", true);
        } else {
            retMap.put("errorMsg", "更新失败");
        }
        return retMap;
    }

    /**
     * 根据修改的字段获取对应的承诺书
     * @param params 参数Map
     * @return 查询结果
     */
    @RequestMapping(value = "appinvs_invs_settings.do/promise", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getPromiseTxt(@RequestBody Map<String, Object> params) {
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("errCode", -1);
        if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId")) || CheckUtil.isEmpty(params.get("column"))) {
            return retMap;
        }
        DtInvsTxtStorageEntity entity = invsSettingsService.getTxtByColumn(params.get("column").toString());
        if (null != entity) {
            retMap.put("txtBean", entity);
            retMap.put("errCode", 0);
        }
        return retMap;
    }


    private boolean checkBean(SettingModifyBean modifyBean) {
        if (StringUtils.isBlank(modifyBean.getModifyReason())) {
            LOGGER.error("InvsSettingsController#modifyInvsSettings 修改理由modifyReason为空。");
            return false;
        }if (modifyBean.getModifyReason().length() > 255) {
            LOGGER.error("InvsSettingsController#modifyInvsSettings 修改理由modifyReason过长，请在120字以内描述。");
            return false;
        }
        if (null == modifyBean.getModifyValue()) {
            LOGGER.error("InvsSettingsController#modifyInvsSettings 修改值modifyValue为空。");
            return false;
        }
        if (null == modifyBean.getModifyProperty()) {
            LOGGER.error("InvsSettingsController#modifyInvsSettings 修改字段modifyProperty为空。");
            return false;
        }
        return true;
    }
}
